import { combineReducers } from 'redux-immutable';

import common from './common';
import userInfo from './userInfo';
import system from './system';
import viewport from './viewport';
import mapInfo from './mapInfo';
import menu from './menu';
import layer from './layer';

const modules = {
    common,
    userInfo,
    system,
    viewport,
    mapInfo,
    menu,
    layer
};

const actionCreatorsSet = {};
const reducers = {};
const sagas = [];

Object.keys(modules).forEach((module) => {
    const { reducer, saga, actionCreators } = modules[module];
    actionCreatorsSet[module] = actionCreators;

    if (reducer) {
        reducers[module] = reducer;
    }

    if (saga) {
        sagas.push(saga);
    }
});

function* rootSaga() {
    for (let s = 0; s < sagas.length; s++) {
        const saga = sagas[s];
        yield* saga();
    }
}

const rootReducer = combineReducers(reducers);

export default {
    actionCreators: actionCreatorsSet,
    rootReducer,
    rootSaga,
};
